package edu.uwosh.cs342.project3;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class QuizList extends ListActivity {
	
	String S;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.quizlist);
        DBHelper dbh = new DBHelper();
        DBHelper.getQuizzesTask asynctask = dbh.new getQuizzesTask();
        asynctask.execute();
        
        String output = "";
        
        
        try {
			output= asynctask.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Toast.makeText(this, output, Toast.LENGTH_LONG).show();
        
        String delims = "[|]";
        String[] quizNames = output.split(delims);
        
       
        
        
        
        //quizNames = {output};
        
        //  Sample names for quizes  
        //    to be changed for to get all the available quizes from the db
        //String [] quizNames = new String[] {"E1 Quiz", "E2 Quiz", "E3 Quiz", "E4 Quiz"};
        
        setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, quizNames));
        
    }

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		Object o = this.getListAdapter().getItem(position);
		String quizName = o.toString();
		
		//  Show the item clicked
		Toast.makeText(this, "you chose " + quizName, Toast.LENGTH_LONG).show();
		
		//  Move to activity with list of questions from quiz
		Intent questionListIntent = new Intent("android.intent.action.QUESTIONLIST");
		questionListIntent.putExtra("quizSelected", quizName);
		startActivity(questionListIntent);
		
	}
}
